/******************************************************
 *** Javascript for language control ******************
 ******************************************************/
// Global variables
var _LanguageTimeout = 5000;
var _LanguageControlServerScriptPath = "./scripts/php/controlLanguage.php";
var _LanguageControlType = "nav";
var _LanguageControlId = "controlLanguage";
var _LanguageContainerId = "controlLanguageContainer";
var _SelectedLanguage = null;

// On load DOM structure
$(document).ready(function() {
	// Set the ready event
	$(_LanguageControlType + "#" + _LanguageControlId).ready(function() {
		// Get the browser language
		_SelectedLanguage = (navigator.language) ? navigator.language : navigator.userLanguage;
		
		// Perform an ajax call to the server
	    $.ajax({
	        type: "POST",
	        url: _LanguageControlServerScriptPath,
	        data: _LanguageControlId + "=" + _SelectedLanguage,
	        success: function(json) {
	        	// Set the content
        		SetContent(json);
        		
        		// Set the chart
        		SetChart(json);
        		
        		// Set the popup
        		//SetPopup(json); The pop-up is disabled. Uncomment this line to enable the pop-up
	        },
	        timeout : _LanguageTimeout // Timeout of the ajax call
	    });					
	});
	
	// Set the click event
	$(_LanguageControlType + "#" + _LanguageControlId).click(function(e) {
		// Cancel the default click behavior
		e.preventDefault();

		// Get the selected language
		_SelectedLanguage = e.target.id;
		
		// Perform an ajax call to the server
		$.ajax({
			type: "POST",
			url: _LanguageControlServerScriptPath,
			data: _LanguageControlId + "=" + _SelectedLanguage,
			success: function(json) {
				// Set the content
        		SetContent(json);
        		
        		// Set the chart
        		SetChart(json);				        	
	        },
			timeout : _LanguageTimeout // Timeout of the ajax call
		});
	});
	
	// Set the orientation change event
	$(window).on("orientationchange", function() {
		// Perform an ajax call to the server
	    $.ajax({
	        type: "POST",
	        url: _LanguageControlServerScriptPath,
	        data: _LanguageControlId + "=" + _SelectedLanguage,
	        success: function(json) {
        		// Set the chart
        		SetChart(json);        		
	        },
	        timeout : _LanguageTimeout // Timeout of the ajax call
	    });
	});
});

// Global functions
function SetContent(json) {
	// Get the json content
	var dictionary = jQuery.parseJSON(json);
	
	// Set the content of unusual elements
	$(document).find("title").html(dictionary.applicationName + " - " + dictionary.applicationSlogan);
	
	// For all elements
	for (var dictionaryEntry in dictionary) {
		// Set the content
		$("#" + dictionaryEntry).html(dictionary[dictionaryEntry]);
		
		// Check the input type
		if ($("input#" + dictionaryEntry).attr("type") == "submit")
			// Set the content for form inputs
			$("input#" + dictionaryEntry).attr("value", dictionary[dictionaryEntry]);	
		else
			// Set the content for form inputs
			$("input#" + dictionaryEntry).attr("placeholder", dictionary[dictionaryEntry]).blur();
				
		// Set the content for form textareas
		$("textarea#" + dictionaryEntry).attr("placeholder", dictionary[dictionaryEntry]).val("").blur();			
	}
}
